package com.education.project.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.education.project.model.WebNavResources;
import com.education.project.service.WebNavResourcesService;

@Controller
@RequestMapping("/system/webnavresources")
public class WebNavResourcesController {
	@Autowired
	private WebNavResourcesService webNavResourcesService;

	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView getIndexPage() {
		ModelAndView model = new ModelAndView("webnavresources/index");
		List rootList = webNavResourcesService.getResourcesByRoot();
		model.addObject("rootList", rootList);
		return model;
	}
	
	@RequestMapping(value="/index",method=RequestMethod.POST)
	public @ResponseBody List getall() {
		return webNavResourcesService.getResourcesByNode();
	}
	
	@RequestMapping(value="/get_nav_image_list",method=RequestMethod.POST)
	public @ResponseBody String[] getNavImageFilenameList(HttpServletRequest request) throws IOException {
		Resource[] resources =  new PathMatchingResourcePatternResolver().getResources("classpath:static/navimage/*");
		String[] fileList = new String[resources.length];
		for(int i=0;i<resources.length;i++) {
			fileList[i] = resources[i].getFilename();
		}
		return fileList;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Map addWebNavResources(@Valid @ModelAttribute("webNavResources") WebNavResources webNavResources,
			BindingResult binder) {
		Map result = new HashMap();
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		if(webNavResources.getOrdernum()==null) {
			webNavResources.setOrdernum(1);
		}
		webNavResourcesService.saveWebNavResources(webNavResources);
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody Map updateWebNavResources(@Valid @ModelAttribute("webNavResources") WebNavResources webNavResources,
			BindingResult binder) {
		Map result = new HashMap();
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		WebNavResources nav = webNavResourcesService.getWebNavResources(webNavResources.getId());
		if(nav!=null) {
			if(webNavResources.getOrdernum()==null) {
				nav.setOrdernum(1);
			}
			nav.setName(webNavResources.getName());
			nav.setParentid(webNavResources.getParentid());
			nav.setImageurl(webNavResources.getImageurl());
			nav.setUrl(webNavResources.getUrl());
			nav.setOrdernum(webNavResources.getOrdernum());
			webNavResourcesService.updateWebNavResources(nav);
		}
		result.put("success", true);
		return result;
	}
	
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody Map removeWebNavResources(@RequestParam("id") Integer id) {
		Map result = new HashMap();
		Integer count = webNavResourcesService.getChildrenNavCount(id);
		if(count.intValue()>0) {
			result.put("success", false);
			result.put("message", "资源无法删除,该资源下还有子节点!");
			return result;
		}
		Integer count1 = webNavResourcesService.getNavResourcesCountByNav(id);
		if(count1.intValue()>0) {
			result.put("success", false);
			result.put("message", "资源无法删除,该资源被用户类别引用!");
			return result;
		}
		webNavResourcesService.deleteWebNavResources(id);
		result.put("success", true);
		return result;
	}
}
